using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 魅惑Buff - 使目标暂时成为施法者的盟友
/// </summary>
public class CharmBuff : BuffBase
{
    private bool m_OriginalIsEnemy;
    private Entity m_OriginalTarget;

    public CharmBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始敌对状���
        // 这里需要根据具体的阵营系统来保存状���
        // m_OriginalIsEnemy = Target.IsEnemy(Caster);
        
        // 设置目标为施法者的盟友
        // Target.SetAlly(Caster);
        
        // 播放魅惑特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("charmed", true);
        }
        
        Logger.Log($"{Target.name} 被魅惑了，成���{Caster.name} 的盟���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 确保目标持续保持魅惑状���
        // 这里可以添加一些特殊行为，比如攻击原来的盟���
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log($"{Target.name} 魅惑效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始敌对状���
        // if (m_OriginalIsEnemy)
        // {
        //     Target.SetEnemy(Caster);
        // }
        // else
        // {
        //     Target.SetNeutral(Caster);
        // }
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("charmed", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Charm"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 魅惑效果不能叠加，只能刷新持续时���
    }
}
